//package makabakaquestionnaire.zutsoftware.framework.cloud;
//
//import com.aliyun.oss.ClientException;
//import com.aliyun.oss.OSSClient;
//import com.aliyun.oss.OSSException;
//import com.aliyun.oss.model.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import zut.cs.software.en.dao.UserDao;
//import zut.cs.software.en.entity.User;
//
//import java.io.File;
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.UUID;
//
//@Service
//
//public class AliyunOssUtil {
//
//    @Autowired
//    private  AliyunOssProperties aliyunOssProperties;
//    @Autowired
//    private UserDao userDao;
//
//
//
//    /**
//     * 上传保存地址到user表
//     * @param file
//     * @return
//     */
//    public String upload(File file,String phone){
//        System.out.println("=======>OSS文件上传开始："+file.getName());
//        String endpoint = aliyunOssProperties.getEndpoint();
//        String accessKey = aliyunOssProperties.getKeyid();
//        String accessKeySecert = aliyunOssProperties.getKeysecret();
//        String bucketName = aliyunOssProperties.getBucketname();
//        String fileHost = aliyunOssProperties.getFilehost();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        String dateStr = format.format(new Date());
//        User userByUserPhone = userDao.findUserByUserPhone(phone);
//
//        System.out.println(userByUserPhone);
//
//        if(null==file){
//            return null;
//        }
//        OSSClient ossClient = new OSSClient(endpoint,accessKey,accessKeySecert);
//        try {
//            //容器不存在，就创建
//            if (!ossClient.doesBucketExist(bucketName)) {
//                ossClient.createBucket(bucketName);
//                CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
//                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
//                ossClient.createBucket(createBucketRequest);
//            }
//            //创建文件路径
//            String fileUrl = fileHost + "/" + phone + "/" + (dateStr + "/" + UUID.randomUUID().toString().replace("-", "") + "-" + file.getName());
//
//            //上传文件
//            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileUrl, file);
//
//            ObjectMetadata metadata = new ObjectMetadata();
//            metadata.setContentType("image/jpg");
//            putObjectRequest.setMetadata(metadata);
//
//            PutObjectResult result = ossClient.putObject(putObjectRequest);
//            //设置权限，这里公开读
//            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
//            if (result != null) {
//                System.out.println("=========>OSS文件上传成功，OSS地址：" + fileUrl);
//                userByUserPhone.setUserAvatar("https://employment-nearby.oss-cn-beijing.aliyuncs.com/"+fileUrl);
//                userDao.save(userByUserPhone);
//                return fileUrl;
//            }
//        }catch (OSSException oe){
//            System.out.println(oe.getMessage());
//        }catch (ClientException ce){
//            System.out.println(ce.getMessage());
//        }finally {
//            //关闭
//            ossClient.shutdown();
//        }
//        return  null;
//    }
//
//    /**
//     * 上传不保存地址到user表
//     * @param file
//     * @return
//     */
//    public String uploadNoUser(File file,String phone){
//        System.out.println("=======>OSS文件上传开始："+file.getName());
//        String endpoint = aliyunOssProperties.getEndpoint();
//        String accessKey = aliyunOssProperties.getKeyid();
//        String accessKeySecert = aliyunOssProperties.getKeysecret();
//        String bucketName = aliyunOssProperties.getBucketname();
//        String fileHost = aliyunOssProperties.getFilehost();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        String dateStr = format.format(new Date());
//        User userByUserPhone = userDao.findUserByUserPhone(phone);
//
//        System.out.println(userByUserPhone);
//
//        if(null==file){
//            return null;
//        }
//        OSSClient ossClient = new OSSClient(endpoint,accessKey,accessKeySecert);
//        try {
//            //容器不存在，就创建
//            if (!ossClient.doesBucketExist(bucketName)) {
//                ossClient.createBucket(bucketName);
//                CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
//                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
//                ossClient.createBucket(createBucketRequest);
//            }
//            //创建文件路径
//            String fileUrl = fileHost + "/" +phone+"/"+ (dateStr + "/" + UUID.randomUUID().toString().replace("-", "") + "-" + file.getName());
//
//            //上传文件
//            PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName, fileUrl, file));
//            //设置权限，这里公开读
//            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
//            if (result != null) {
//                System.out.println("=========>OSS文件上传成功，OSS地址：" + fileUrl);
////                userByUserPhone.setUserAvatar(fileUrl);
////                userDao.save(userByUserPhone);
//                return fileUrl;
//            }
//        }catch (OSSException oe){
//            System.out.println(oe.getMessage());
//        }catch (ClientException ce){
//            System.out.println(ce.getMessage());
//        }finally {
//            //关闭
//            ossClient.shutdown();
//        }
//        return  null;
//    }
//    /**
//     * 删除文件
//     * @param fileKey
//     * @return
//     */
//    public String deleteBlog(String fileKey){
//        System.out.println("======>OSS文件删除开始");
//        String endpoint = aliyunOssProperties.getEndpoint();
//        String accessKeyId = aliyunOssProperties.getKeyid();
//        String accessKeySecret = aliyunOssProperties.getKeysecret();
//        String bucketName = aliyunOssProperties.getBucketname();
//        String fileHost = aliyunOssProperties.getFilehost();
//
//        try {
//            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//
//            if (!ossClient.doesBucketExist(bucketName)) {
//                System.out.println("========>您的Bucket不存在");
//                return "您的Bucket不存在";
//            } else {
//                System.out.println("========>开始删除Object");
//                ossClient.deleteObject(bucketName, fileKey);
//                System.out.println("========>Object删除成功：" + fileKey);
//                return "============>Object删除成功：" + fileKey;
//            }
//        }catch (Exception ex){
//            System.out.println("删除object失败"+ex);
//            return "删除Object失败";
//        }
//    }
//
//    /**
//     * 查询所有
//     * @param bucketName
//     * @return
//     */
//    public List<String> getObjectList(String bucketName,String phone){
//        List<String> listRe = new ArrayList<>();
//        String endpoint = aliyunOssProperties.getEndpoint();
//        String accessKeyId = aliyunOssProperties.getKeyid();
//        String accessKeySecret = aliyunOssProperties.getKeysecret();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        String dateStr = format.format(new Date());
//        try {
//            System.out.println("=========>查询文件列表");
//            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
//            //列出bucketName目录下所有的文件
//            listObjectsRequest.setPrefix("en/"+phone+"/" );
//            ObjectListing list = ossClient.listObjects(listObjectsRequest);
//            for (OSSObjectSummary objectSummary : list.getObjectSummaries()) {
//                System.out.println(objectSummary.getKey());
//                listRe.add(objectSummary.toString());
//            }
//            return listRe;
//        }catch (Exception ex){
//            System.out.println("========>查询列表失败"+ex);
//            return new ArrayList<>();
//        }
//    }
////
////    public String getObjectList(String bucketName,String key){
////
////        String endpoint = aliyunOssProperties.getEndpoint();
////        String accessKeyId = aliyunOssProperties.getKeyid();
////        String accessKeySecret = aliyunOssProperties.getKeysecret();
////        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
////        String dateStr = format.format(new Date());
////        try {
////            System.out.println("=========>查询文件列表");
////            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
////            ObjectsRequest listObjectsRequest = new ObjectsRequest(bucketName);
////            //列出bucketName目录下所有的文件
////            listObjectsRequest.setPrefix("en/"+key+"/" );
////            ObjectListing list = ossClient.listObjects(listObjectsRequest);
////            for (OSSObjectSummary objectSummary : list.getObjectSummaries()) {
////                System.out.println(objectSummary.getKey());
////                listRe.add(objectSummary.toString());
////            }
////            return listRe;
////        }catch (Exception ex){
////            System.out.println("========>查询列表失败"+ex);
////            return new ArrayList<>();
////        }
////    }
//
////    public void Symlink() {
////        // Endpoint以杭州为例，其它Region请按实际情况填写。
////        String endpoint = aliyunOssProperties.getEndpoint();
////        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
////        String bucketName = aliyunOssProperties.getBucketname();
////        String accessKeyId = aliyunOssProperties.getKeyid();
////        String accessKeySecret = aliyunOssProperties.getKeysecret();
////        String symLink = "<yourSymLink>";
////
////        // 创建OSSClient实例。
////        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
////
////        // 获取软链接。
////        OSSSymlink symbolicLink = ossClient.getSymlink(bucketName, symLink);
////// 打印软链接指向的文件内容。
////        System.out.println(symbolicLink.getSymlink());
////        System.out.println(symbolicLink.getTarget());
////        System.out.println(symbolicLink.getRequestId());
////
////// 关闭OSSClient。
////        ossClient.shutdown();
////    }
//}
